package com.auto.learning.activity;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.EditText;
import android.widget.RelativeLayout;

import com.auto.learning.R;
import com.auto.learning.event.Handle;
import com.auto.learning.net.HttpRequestHelper;
import com.auto.learning.tools.CommonMethod;
import com.auto.learning.view.SharedPreferencesUtils;

public class ChangePasswordActivity extends BaseActivity {
	
	private EditText et_oldpassword,et_newpassword,et_rep_password;
	private RelativeLayout rel_ok;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
//		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_changepassword);
		initView();
	}

	private void initView() {
		setTitle(getResources().getString(R.string.favor_06));
		et_oldpassword = (EditText) findViewById(R.id.et_oldpassword);
		et_newpassword = (EditText) findViewById(R.id.et_newpassword);
		et_rep_password = (EditText) findViewById(R.id.et_rep_password);
		rel_ok = (RelativeLayout) findViewById(R.id.rel_ok);
		rel_ok.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				changePassword();
			}
		});
	}
	
	
	private void changePassword(){
		String oldPassword = et_oldpassword.getText().toString();
		String newPassword = et_newpassword.getText().toString();
		String rep_newPassword = et_rep_password.getText().toString();
		if (TextUtils.isEmpty(oldPassword)) {
			CommonMethod.makeNotice(this, "原密码不能为空");
			return;
		}
		if (TextUtils.isEmpty(newPassword) || TextUtils.isEmpty(rep_newPassword)) {
			CommonMethod.makeNotice(this, "新密码不能为空");
			return;
		}
		/*Pattern password = Pattern.compile("^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,20}$");// 密码正则表达式数字和字母
		if (!password.matcher(newPassword).matches()) {
			CommonMethod.makeNotice(this, "密码格式不正确，密码由数字和字母组成");
			return;
		}*/
		if (!newPassword.equals(rep_newPassword)) {
			CommonMethod.makeNotice(this, "两次输入的密码不一致");
			return;
		}
		changePwd(this,oldPassword,newPassword);
		
	}
	
	private void changePwd(Context context,String oldPassword,String newPassword){
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair(HttpRequestHelper.USERID, String.valueOf(SharedPreferencesUtils.getParam(context, HttpRequestHelper.USERID, 0))));
		params.add(new BasicNameValuePair("oldPwd", oldPassword));
		params.add(new BasicNameValuePair("newPwd", newPassword));
		HttpRequestHelper.executeHttpDataNoData(context, HttpRequestHelper.CHANGE_PWD, params, handle, HttpRequestHelper.GETDATA_SUCCESS, HttpRequestHelper.GETDATA_FAIL);
	}
	
	private Handle handle = new Handle(this){
		public void handleMessage(android.os.Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
			case HttpRequestHelper.GETDATA_SUCCESS:
				CommonMethod.makeNotice(ChangePasswordActivity.this, "修改成功");
				finish();
				break;
			case HttpRequestHelper.GETDATA_FAIL:
				CommonMethod.makeNotice(ChangePasswordActivity.this, msg.obj.toString()+"");
				break;
			}
		};
	};
}
